#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<getopt.h>
#include<netdb.h>
#include<dirent.h>
#include<signal.h>
#include<syslog.h>
#include<time.h> 
#include<strings.h>

int			g_stop = 0;

void sig_handler(int signum);
void sig_code(int signum);//信号
void dns(char *domain_name, char **ip);
int get_sys_time(char *datime);//获取系统时间
int ds18b20_get_temperature(float *temp);//获取温度

void dns(char *domain_name, char **ip)
{
	struct hostent 		*server_name = NULL;

	server_name = gethostbyname(domain_name);
	inet_ntop(server_name->h_addrtype,server_name->h_addr, *ip, 32);
}


void sig_handler(int signum)
{
	if(SIGTERM==signum)
	{
		printf("SIGTERM signal detected\n");
		g_stop=1; 
	}
	else if(SIGALRM==signum)
	{
		printf("SIGALRM signal detected\n");
		g_stop=1;
	}
	else if(SIGINT==signum)
	{
		printf("SIGINT signal detected\n");
		g_stop=1;
	}
}
 
void sig_code(int signum)
{
	if(SIGBUS==signum)
	{
		printf("SIGBUS signal detected\n");
	}
	else if(SIGILL==signum)
	{
		printf("SIGILL signal detected\n");
	}
	else if(SIGSEGV==signum)
	{
		printf("SIGSEGV signal detected\n");
	}
	exit(1);
}

int get_sys_time(char *datime)
{
	time_t seconds;
	struct tm *pTM;
	     
	time(&seconds);
	pTM=localtime(&seconds);
	sprintf(datime,"%04d-%02d-%02d  %02d:%02d:%02d\n",pTM->tm_year+1900,pTM->tm_mon+1,pTM->tm_mday,pTM->tm_hour,pTM->tm_min,pTM->tm_sec);
	return 0; 
}


int ds18b20_get_temperature(float *temp)
{
	char		w1_path[50] = "/sys/bus/w1/devices/";
	char		chip[20];
	char		buf[128];
	DIR		*dirp;
	struct dirent	*direntp;
	int 		fd = -1;
	char		*ptr;
	float		value;
	int		found = 0;

	if( !temp )
	{
		return -1;
	}

	if( (dirp = opendir(w1_path)) == NULL )
	{
		printf("opendir error : %s\n", strerror(errno));
	}

	while( (direntp = readdir(dirp)) != NULL )
	{
		if(strstr(direntp->d_name, "28-"))
		{
			strcpy(chip, direntp->d_name);
			found = 1;
			break;
		}
	}
	closedir(dirp);

	if( !found )

	{
		printf("Can not find ds18b20 in %s\n", w1_path);
		return -3;
	}

	strncat(w1_path, chip, sizeof(w1_path) - strlen(w1_path));
	strncat(w1_path, "/w1_slave", sizeof(w1_path) - strlen(w1_path));

	if( (fd = open(w1_path, O_RDONLY)) < 0 )
	{
		printf("open %s error : %s\n", w1_path, strerror(errno));
		return -4;
	}

	if( read(fd, buf, sizeof(buf)) < 0 )
	{
		printf("read %s error : %s\n", w1_path, strerror(errno));
                return -5;
	}

	ptr = strstr(buf, "t=");
	if( !ptr )
	{
		printf("ERROR : Can not get temperature\n");
		return -6;
	}

	ptr+=2;

	*temp = atof(ptr)/1000.0;

	close(fd);
	return 0;
}


int main(int argc, char **argv)
{
	int 			server_fd = -1;
	int 			rv = -1;
	struct sockaddr_in	servaddr;
	char 			*servip = "192.168.0.16";
	int 			port = 8889;
	char 			buf[1024];
	int			ch = -1;
	float			temp;
	char 			datime[32];
	char			*user = "Linke";
	int			connected = 0;


	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(server_fd < 0)
	{
		printf("Create client socket unsuccessfully : %s\n", strerror(errno));
		return -1;
	}
	printf("Create client socket[%d] successfully!\n", server_fd);//socket部分

	memset(&servaddr, 0, sizeof(servaddr));//清空结构体servaddr
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	inet_aton(servip, &servaddr.sin_addr);//将服务器的网络类型，地址和端口放入结构体中

	signal(SIGTERM, sig_handler);
	signal(SIGALRM, sig_handler);
	signal(SIGINT, sig_handler);
	signal(SIGBUS, sig_code);	
	signal(SIGILL, sig_code);
	signal(SIGSEGV, sig_code);//安装信号

	while(!g_stop)
	{
		get_sys_time(datime);
		ds18b20_get_temperature(&temp);
		memset(buf, 0, sizeof(buf));
		snprintf(buf,sizeof(buf),"%s\n/%s/%f ",user,datime,temp);
		//printf("user : %s\ntime : %stemperature : %f\n", user, datime, temp);

		if( !connected )
		{


			rv = connect(server_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
			if(rv < 0)
			{
				printf("Connect to server[%s:%d] unsuccessfully : %s\n", servip, port, strerror(errno));
			sleep(5);
			}
			printf("Connect to server[%s:%d] successfully!\n", servip, port);//connect部分
           	}
		rv = write(server_fd, buf, strlen(buf));
		if(rv < 0)
		{
			printf("Write to server[%d] unsuccessfully : %s\n", server_fd, strerror(errno));
			return -1;
			break;
		}
		printf("Write to server[%d] successfully!\n", server_fd);//write部分
                
	       	memset(buf, 0, sizeof(buf));
 		rv = read(server_fd, buf, sizeof(buf));
		if(rv < 0)
		{
			connected = 1;
			printf("Read data from server[%d] unsuccessfully : %s\n", server_fd, strerror(errno));
			sleep(5);
			continue;
		}
		else if(rv == 0)
		{
			printf("Server[%d] get disconnected!\n", server_fd);
			return -1;
			break;
		}
		else if(rv > 0)
		{
			connected = 1;
			printf("Read %d bytes data : %s", rv, buf);
		}//read部分

		sleep(10);

  	}

	close(server_fd);
	return 0;
}

